<?php
	session_start();
	include('db-api/txt-db-api.php');
	
	function Add($id)
	{
		$db = new Database('Eclass');
		$rs = $db->executeQuery('SELECT * FROM UsersCourses WHERE UserName = \'' .
		$_SESSION['username'] . '\' AND CourseId = ' . $id);
		
		if($rs->next())
			return;
		$db->executeQuery('INSERT INTO UsersCourses VALUES (' . $_SESSION['username'] .
		', ' . $id . ', false)');
	}
	
	function Remove($id)
	{
		$db = new Database('Eclass');
		$db->executeQuery('DELETE FROM UsersCourses WHERE UserName = \'' . $_SESSION['username'] .
		'\' AND CourseId = ' . $id);
	}
	
	if(isset($_SESSION['username']))
	{
		if($_GET['action'] == 'add' && isset($_GET['id']))
			Add($_GET['id']);
		else if($_GET['action'] == 'remove' && isset($_GET['id']))
			Remove($_GET['id']);
	}
	if(isset($_SESSION['previous_page']))
	{
		header('Location: ' . $_SESSION['previous_page'] . '?message=1');
	}
	else
		header('Location: index.php');
?>